package org.linlinjava.litemall.admin.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.LitemallUserMemberMapper;
import org.linlinjava.litemall.db.dao.UserMemberFreezeMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallMemberTimesService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class AdminUserMemberService  {
    @Autowired
    private LitemallUserMemberMapper userMemberMapper;
    @Autowired
    private LitemallUserService userService;
    @Autowired
    private UserMemberFreezeMapper userMemberFreezeMapper;
    @Autowired
    private LitemallMemberTimesService litemallMemberTimesService;

    /**
     * 后台展示所有会员用户
     *
     * @param nickname
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public Object listAll(String nickname, Integer page, Integer limit, String sort, String order,String mobilePhone,String[]  effective)  {
        LitemallUserMemberExample example = new LitemallUserMemberExample();
        LitemallUserMemberExample.Criteria criteria = example.or();
        criteria.andDeletedEqualTo(false)
                .andEffectiveEqualTo(true);
        if (!StringUtils.isEmpty(nickname)) {
            criteria.andNicknameLike("%" + nickname + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(("l."+sort + " " + order).replaceAll("\\)",""));
        }

        PageHelper.startPage(page, limit);

        List<LitemallUserMember> litemallUserMemberList = userMemberMapper.getListAndUser(example,nickname,mobilePhone,effective,null);
//        for (LitemallUserMember userMember : litemallUserMemberList) {
//            Map<String, Object> map = new HashMap<>();
//            LitemallUser user = userService.findById(userMember.getUserId());
//            map.put("user", user);
//            map.put("userMember", userMember);
//            mapList.add(map);
//        }
        for (LitemallUserMember orderInfo : litemallUserMemberList){
            UserMemberFreeze freeze = userMemberFreezeMapper.selectOne(Wrappers.<UserMemberFreeze>lambdaQuery().
                    eq(UserMemberFreeze::getUserId, orderInfo.getUserId())
                    .eq(UserMemberFreeze::getMemberId, orderInfo.getId())
                    .eq(UserMemberFreeze::getState, "0")
            );
            if (ObjectUtil.isNotEmpty(freeze)){
                orderInfo.setFreeDays(freeze.getFreezeDays()/3600/24);
            }
            LitemallMemberTimes one = litemallMemberTimesService.getOne(
                    Wrappers.<LitemallMemberTimes>lambdaQuery()
                            .eq(LitemallMemberTimes::getUserId, orderInfo.getUserId())
                            .eq(LitemallMemberTimes::getOrderId, orderInfo.getId())
            );
            if (ObjectUtil.isEmpty(one)) one=new LitemallMemberTimes();
            orderInfo.setTimes(one);
        }
        return ResponseUtil.okList( litemallUserMemberList);
    }


    public Object list(Integer userId,String effective) {
        return ResponseUtil.ok(userMemberMapper.getUserMemberList(userId,effective));
    }
    /**
     * 获取删除会员信息
     * @param nickname
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public Object listDeleteAll(String nickname, Integer page, Integer limit, String sort, String order) {
        LitemallUserMemberExample example = new LitemallUserMemberExample();
        LitemallUserMemberExample.Criteria criteria = example.or();
        criteria.andDeletedEqualTo(true)
                .andEffectiveEqualTo(false);
        if (!StringUtils.isEmpty(nickname)) {
            criteria.andNicknameLike("%" + nickname + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<LitemallUserMember> litemallUserMemberList = userMemberMapper.selectByExampleSelective(example);
        for (LitemallUserMember userMember : litemallUserMemberList) {
            Map<String, Object> map = new HashMap<>();
            LitemallUser user = userService.findById(userMember.getUserId());
            map.put("user", user);
            map.put("userMember", userMember);
            mapList.add(map);
        }
        return ResponseUtil.okList(mapList, litemallUserMemberList);
    }


    public Object getById(Integer id){
        List<LitemallUserMember> litemallUserMemberList = userMemberMapper.getListAndUser(null,null,null,null,id);
        return ResponseUtil.ok(litemallUserMemberList.get(0));
    }
}
